package com.arrogames.blondbattle {
	import com.arrogames.blondbattle.controller.MainController;
	import com.arrogames.blondbattle.game.GameEngine;
	import com.arrogames.blondbattle.ui.BBNavigator;
	import com.arrogames.blondbattle.ui.GameScreen;
	import com.arrogames.blondbattle.ui.LoadingDisplay;
	import com.arrogames.blondbattle.ui.MenuScreen;
	import com.arrogames.blondbattle.xml.UnitParser;
	import com.arrogames.blondbattle.xml.WorldParser;
	import com.greensock.easing.Elastic;
	import com.greensock.TweenMax;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.controls.text.TextFieldTextRenderer;
	import feathers.core.FeathersControl;
	import feathers.core.ITextRenderer;
	import feathers.data.ListCollection;
	import flash.text.TextFormat;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	
	
	
	public class BBMain extends Sprite {
		
		//View components
		private var navigator:BBNavigator;
		private var gameDisplay:GameScreen;
		private var menuDisplay:MenuScreen;
		private var loadingDisplay:LoadingDisplay;
		
		//Model components
		private var game:GameEngine;
		private var model:GameModel;
		
		//Controller component
		private var controller:MainController;
		
		public function BBMain() {
			//new MinimalMobileTheme();
			FeathersControl.defaultTextRendererFactory = function():ITextRenderer {
				var textRenderer:TextFieldTextRenderer = new TextFieldTextRenderer();
				textRenderer.textFormat = new TextFormat( "comic", 10, 0x000000 );
				textRenderer.embedFonts = true;
				return textRenderer;
			};
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		private function onAddedToStage(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			loadingDisplay = addChild(new LoadingDisplay()) as LoadingDisplay;
			
		}
		
		public function assetProgress(ratio:Number):void {
			loadingDisplay.assetProgress(ratio);
		}
		
		public function initialize():void {
			loadingDisplay.dispose();
			removeChild(loadingDisplay);
			loadingDisplay = null;
			
			//Setup the navigator and screens.
			navigator = new BBNavigator();
			addChild(navigator);
			
			gameDisplay = new GameScreen();
			menuDisplay = new MenuScreen();
			
			//create the available units
			var availableUnits:ListCollection = new ListCollection(UnitParser.parse())
			
			//create the available units
			var availableWorlds:ListCollection = new ListCollection(WorldParser.parse())
			
			//Setup the game and model
			game = new GameEngine(gameDisplay, availableUnits);
			model = new GameModel(game, availableWorlds);
			model.availableUnits = availableUnits;

			//add the screens
			navigator.addScreen(BBNavigator.MAIN_MENU, new ScreenNavigatorItem(menuDisplay,null,{model:model}));
			navigator.addScreen(BBNavigator.GAME_FIELD, new ScreenNavigatorItem(gameDisplay));
			
			//Setup the controller
			controller = new MainController(navigator, model);
		}
	}
}